package com.matt.sooncleaner.junk

import android.content.Context
import android.graphics.Color
import androidx.core.view.WindowCompat
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.BarUtils
import com.google.android.material.divider.MaterialDividerItemDecoration
import com.matt.sooncleaner.R
import com.matt.sooncleaner.arch.ArchFragment
import com.matt.sooncleaner.databinding.FragmentJunkListBinding

class JunkScanResultFragment: ArchFragment<FragmentJunkListBinding>() {
    interface StartCleanListener {
        fun startClean(result: MutableList<JunkBean>)
    }

    private val mJunkList = mutableListOf<JunkBean>()
    private val mAdapter = JunkAdapter(mutableListOf())
    private var mStartCleanListener: StartCleanListener? = null

    override fun onAttach(context: Context) {
        super.onAttach(context)
        if (context is StartCleanListener) {
            mStartCleanListener = context
        }
    }

    override fun onInitView(binding: FragmentJunkListBinding) {
        super.onInitView(binding)
        BarUtils.transparentStatusBar(requireActivity().window)
        BarUtils.setStatusBarColor(requireActivity(), resources.getColor(R.color.colorTheme))
        BarUtils.setStatusBarLightMode(requireActivity(), false)
        binding.statusBarView.layoutParams.height = BarUtils.getStatusBarHeight()
        binding.toolbar.setNavigationOnClickListener { requireActivity().finish() }
        binding.recyclerView.layoutManager = LinearLayoutManager(context)
        binding.recyclerView.addItemDecoration(MaterialDividerItemDecoration(requireContext(), DividerItemDecoration.VERTICAL).apply {
            dividerColor = resources.getColor(R.color.colorDivider)
            dividerInsetStart = 0
            dividerInsetEnd = 0
            isLastItemDecorated = false
        })
        binding.recyclerView.adapter = mAdapter
        mAdapter.submitList(mJunkList)
        binding.cleanBtn.setOnClickListener {
            mStartCleanListener?.startClean(mJunkList)
        }
    }

    fun setJunkResult(result: MutableList<JunkBean>) {
        mJunkList.addAll(result)
    }
}